rm(list = ls())

path <-  getwd()

# packages
pacman::p_load(tidyverse, plotly,readxl,scales, 
               extrafont,PerformanceAnalytics,  GGally, 
               patchwork, ggpubr, ggrepel, stargazer)

# Font for windows and mac
if(stringr::str_detect(path, "D:/")){ 
  
  font_import()
  windowsFonts()
  loadfonts(device = "win")
  
  theme_set(theme_gray(base_size = 10, 
                       base_family = "Arial"))        # Windows用
  font_name <- "Arial"

 } else{
  
  theme_set(theme_gray(base_size = 10, 
                       base_family = "HiraginoSans-W3"))  # macOS用
  font_name <- "HiraKakuProN-W3"
 }

1 このファイルについて

  • 『社会保障研究』の2022年12月号掲載の論文に使用した4つのグラフを生成するRMarkdown
  • データの前処理はtime_series_data_for_shahoken.Rmdで作成
  • 生活保護世帯類型別のデータはyoy_hogo_households_receive_by_type.csvを使用
  • 生活保護世帯類型別のグラフ(図4のパネルB)以外のグラフは、functions_time_series_graphs_for_shahoken.R内の関数で作成

2 データの読み込み

2.1 関数の読み込み

source("functions_time_series_graph_for_shahoken.R")

2.2 緊急小口・総合支援と住居確保データ

# 緊急小口・総合支援と住居確保データ
koguchi_sogo_jukyo <- readr::read_csv("output/koguchi_sogo_jukyo.csv")
## Rows: 1296 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (18): id, koguchi_apply, koguchi_number, koguchi_payment_amount, sogo_a...
## date  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
koguchi_sogo_jukyo_percapita <- readr::read_csv("output/koguchi_sogo_jukyo_percapita.csv")
## Rows: 1296 Columns: 24
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (2): prefec_kanji, prefec
## dbl  (21): id, koguchi_apply, koguchi_number, koguchi_payment_amount, sogo_a...
## date  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 全国を除く
koguchi_sogo_jukyo_all  <- koguchi_sogo_jukyo  %>% 
  dplyr::filter(id == 99)
# 全国を除く
koguchi_sogo_jukyo_percapita_47prefec  <- koguchi_sogo_jukyo_percapita  %>% 
  dplyr::filter(id != 99)
## 全国 + 都道府県グラフ作成用 2022/6/2

df_koguchi_sogo_jukyo_percapita <- koguchi_sogo_jukyo_percapita %>% 
  dplyr::filter(id == 13 | id == 47 |id == 99) # 13:東京、47:沖縄、99:全国

df_koguchi_sogo_jukyo_zenkoku <- koguchi_sogo_jukyo_percapita %>% 
  dplyr::filter(id == 99)

df_koguchi_sogo_jukyo_tokyo <- koguchi_sogo_jukyo_percapita %>% 
  dplyr::filter(id == 13)

df_koguchi_sogo_jukyo_okinawa <- koguchi_sogo_jukyo_percapita %>% 
  dplyr::filter(id == 47)

df_koguchi_sogo_jukyo_not_main <- koguchi_sogo_jukyo_percapita %>% 
  dplyr::filter(id != 13 & id != 47 & id != 99)

2.3 失業給付データ

# 失業給付
unemp_benefit_number <- readr::read_csv("output/unemp_benefit_number.csv")
## Rows: 2976 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): prefec_kanji, prefec
## dbl  (8): unemp_benefit_number_total, unemp_benefit_number_male, unemp_benef...
## date (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
unemp_benefit_number_percapita <- readr::read_csv("output/unemp_benefit_number_percapita.csv")
## Rows: 1968 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (2): prefec_kanji, prefec
## dbl  (11): id, population_total, population_male, population_female, year, u...
## date  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 全国
unemp_benefit_number_all <- unemp_benefit_number %>% 
  dplyr::filter(id == 99) %>% 
  dplyr::filter(year >= 2018)
# 2019年以降のデータ
unemp_benefit_number_all_2019 <- unemp_benefit_number_all %>% 
  dplyr::filter(date >= "2019-01-01")
# 全国を除く
unemp_benefit_number_percapita_47prefec  <- unemp_benefit_number_percapita %>%
  dplyr::filter(id != 99)
# 2019年以降のデータ
unemp_benefit_number_percapita_47prefec_2019 <- unemp_benefit_number_percapita_47prefec %>% 
  dplyr::filter(date >= "2019-01-01")
## 全国 + 都道府県グラフ作成用 2022/5/26
df_main <- unemp_benefit_number_percapita %>% 
  dplyr::filter(id == 13 | id == 47 |id == 99)

df_not_main <- unemp_benefit_number_percapita %>% 
  dplyr::filter(id != 13 & id != 47 & id != 99)
## 都道府県グラフ作成用 2022/5/26
df_prefecture2 <- unemp_benefit_number_percapita_47prefec %>% 
  dplyr::filter(id == 13 | id == 47)

df_prefecture45 <- unemp_benefit_number_percapita_47prefec %>% 
  dplyr::filter(id != 13 & id != 47 )
## 全国 + 都道府県グラフ作成用 2022/6/2
df_zenkoku <- unemp_benefit_number_percapita %>% 
  dplyr::filter(id == 99)

df_tokyo <- unemp_benefit_number_percapita %>% 
  dplyr::filter(id == 13)

df_okinawa <- unemp_benefit_number_percapita %>% 
  dplyr::filter(id == 47)


df_not_main <- unemp_benefit_number_percapita %>% 
  dplyr::filter(id != 13 & id != 47 & id != 99)

2.4 生活保護データ

# 生活保護
hogo_sum <- readr::read_csv("output/hogo_sum.csv")
## Rows: 2544 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (2): prefec_kanji, prefec
## dbl  (11): households_total, households_receive, households_suspend, persons...
## date  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
hogo_sum_percapita <- readr::read_csv("output/hogo_sum_percapita.csv")
## Rows: 1968 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (2): prefec_kanji, prefec
## dbl  (14): households_total, households_receive, households_suspend, persons...
## date  (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 全国データ
hogo_sum_all <- hogo_sum %>% 
  dplyr::filter(id == 99)
# 全国を除く
hogo_sum_percapita_47prefec  <- hogo_sum_percapita %>% 
  dplyr::filter(id != 99)
# 2019年以降のデータ
hogo_sum_percapita_47prefec_2019 <- hogo_sum_percapita_47prefec %>% 
  dplyr::filter(date >= "2019-01-01")
## 全国 + 都道府県グラフ作成用 2022/6/2
df_hogo_zenkoku <- hogo_sum_percapita %>% 
  dplyr::filter(id == 99)

df_hogo_tokyo <- hogo_sum_percapita %>% 
  dplyr::filter(id == 13)

df_hogo_okinawa <- hogo_sum_percapita %>% 
  dplyr::filter(id == 47)

df_hogo_main <- hogo_sum_percapita %>% 
  dplyr::filter(id == 13 | id == 47 |id == 99)

df_hogo_not_main <- hogo_sum_percapita %>% 
  dplyr::filter(id != 13 &  id != 47 & id != 99)

2.5 生活保護データ(世帯類型別)

df_yoy_hogo_households_by_type <- readr::read_csv("output/yoy_hogo_households_receive_by_type.csv") # Change data frame name, Ando 2022.11.12
## Rows: 52 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (8): year, month, households_receive_total, yoy_households_receive_elde...
## dttm (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
df_yoy_hogo_households_by_type <- df_yoy_hogo_households_by_type %>% 
  dplyr::filter(date >= "2019-01-01") #Mar21

3 図1:住居確保給付金

3.1 グラフの作成と表示

graph_jukyo <- graph_jukyo(outcome = "jukyo_number")

graph_jukyo

3.2 グラフの保存

width_size <- 5

graph_jukyo %>% 
  ggsave(filename = "output/graph_jukyo.png",
         width = width_size,height= width_size*0.7,
         dpi = 500)

3.3 インタラクティブグラフ

ggplotly(graph_jukyo)
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## Please use `gather()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.

4 図2:緊急小口資金と総合支援資金

4.1 グラフの作成

4.1.1 パネルA:緊急小口資金

graph_koguch_patch <- graph_koguchi_sogo_jukyo(outcome = "koguchi_number") + 
  labs(title="パネルA:緊急小口資金") +
  theme(text = element_text(size = 18)) +
  ylim(0,650)

graph_koguch_patch

4.1.2 パネルB:総合支援資金

graph_sogo_patch <- graph_koguchi_sogo_jukyo( outcome = "sogo_number") +
    labs(title="パネルB:総合支援資金") +
  theme(text = element_text(size = 18)) +
  ylim(0,650)

graph_sogo_patch

4.2 グラフの統合と表示

graph_koguchi_sogo_combined <- patchwork::wrap_plots(graph_koguch_patch, 
                      graph_sogo_patch, 
                      nrow = 1)

graph_koguchi_sogo_combined

## グラフの保存

width_size <- 12

graph_koguchi_sogo_combined %>% 
  ggsave(filename="output/graph_koguchi_sogo_combined.png",
         width= width_size,
         height= width_size*0.5,
         dpi = 600)

4.3 インタラクティブグラフ

4.3.1 パネルA:緊急小口資金

graph_zenkoku47_koguch <- graph_koguchi_sogo_jukyo(outcome = "koguchi_number")

ggplotly(graph_zenkoku47_koguch)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

4.3.2 パネルB:総合支援資金

graph_zenkoku47_sogo <- graph_koguchi_sogo_jukyo(outcome = "sogo_number")

ggplotly(graph_zenkoku47_sogo)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

5 図3:失業給付(基本手当)

5.1 グラフの作成

5.1.1 パネルA:男女合計

graph_unemp_benefit_number_total_patch <- graph_unemp_benefit(outcome = "unemp_benefit_number_total") + 
  labs(title="パネルA:合計") +
  theme(text = element_text(size = 18))

graph_unemp_benefit_number_total_patch

5.1.2 パネルB:女性

graph_unemp_benefit_number_female_patch <- graph_unemp_benefit(outcome = "unemp_benefit_number_female") + 
  labs(title="パネルB:女性") +
  theme(text = element_text(size = 18))

graph_unemp_benefit_number_female_patch

5.1.3 パネルC:男性

graph_unemp_benefit_number_male_patch <- graph_unemp_benefit(outcome = "unemp_benefit_number_male") + 
  labs(title="パネルC:男性") +
  theme(text = element_text(size = 18))

graph_unemp_benefit_number_male_patch
## Warning: Removed 6 row(s) containing missing values (geom_path).

## グラフの統合と表示

graph_unemp_benefit_number_combined <- patchwork::wrap_plots(graph_unemp_benefit_number_total_patch, plot_spacer(), graph_unemp_benefit_number_female_patch, 
                      graph_unemp_benefit_number_male_patch, 
                      nrow = 2)

graph_unemp_benefit_number_combined
## Warning: Removed 6 row(s) containing missing values (geom_path).

## グラフの保存

width_size <- 15

graph_unemp_benefit_number_combined %>% 
  ggsave(filename="output/graph_unemp_benefit_number_combined.png",
         width= width_size,
         height= width_size*0.8,
         dpi = 400)
## Warning: Removed 6 row(s) containing missing values (geom_path).

5.2 インタラクティブグラフ

5.2.1 男女合計

ggplotly(graph_unemp_benefit_number_total_patch)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

5.2.2 女性

ggplotly(graph_unemp_benefit_number_female_patch)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

5.2.3 男性

ggplotly(graph_unemp_benefit_number_male_patch)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

6 図表4:生活保護

6.1 グラフの作成

6.1.1 パネルA:10万人あたり利用者数

graph_hogo_zenkoku47_persons_receive <- graph_hogo_persons(outcome = "persons_receive") + 
  labs(title="パネルA:10万人あたり利用者数") +
  theme(text = element_text(size = 10))

graph_hogo_zenkoku47_persons_receive

6.1.2 パネルB:生活保護(世帯類型別)

YOY_public_assistance <- ggplot(data = df_yoy_hogo_households_by_type) + 
  geom_vline(aes(xintercept = as.Date("2020-01-01")),
             colour = "gray", 
             linetype="solid", size = 1.5) +
  
  geom_line(aes(x = as.Date(date), 
                y = yoy_households_receive_elderly, 
                group = 1),
            stat = "identity", 
            position = "identity", 
            color = "gray30") + 
  
  geom_line(aes(x = as.Date(date), 
                y =  yoy_households_receive_singlemother, 
                group = 1),
            stat = "identity", 
            position = "identity", 
            color = "gray30", 
            linetype = "dashed") +
  geom_line(aes(x = as.Date(date), 
                y =  yoy_households_receive_disabled, 
                group = 1),
            stat = "identity", 
            position = "identity", 
            color = "gray30") +
  
  geom_line(aes(x = as.Date(date), 
                y =  yoy_households_receive_sick, 
                group = 1),
            stat = "identity", 
            position = "identity", 
            color = "gray30") +
  
  geom_line(aes(x = as.Date(date), 
                y =  yoy_households_receive_others, 
                group = 1), 
            stat = "identity", 
            position = "identity", 
            color = "gray30", 
            size = 1.3) +
  
  scale_y_continuous(breaks = seq(-10000, 20000, 
                                  by = 5000), 
                     limits = c(-12000,20000)) +
  scale_x_date(labels = scales::date_format("%Y/%m"), 
               breaks= as.Date(datebreaks)) +
  theme_classic(base_family = font_name) +
  theme(legend.position = "none", 
        axis.text.x = element_text(angle = 90 , hjust=1)) +

  annotate(geom = "text", 
           x = as.Date("2019-01-01"), 
           y = 14000, 
           hjust = 0,
           label = "高齢者世帯", 
           size = annotate_size , 
           color = "gray30", 
           family = font_name) +
  annotate(geom = "text", 
           x = as.Date("2019-01-01"), 
           y = 1500, 
           hjust = 0,
           label = "障害者世帯", 
           size = annotate_size , 
           color = "gray30", 
           family = font_name) +
  annotate(geom = "text", 
           x = as.Date("2019-01-01"), 
           y = -3500, 
           hjust = 0,
           label = "母子世帯", 
           size = annotate_size , 
           color = "gray30", 
           family = font_name) +
  annotate(geom = "text", 
           x = as.Date("2020-04-01"), 
           y = -8500, 
           hjust = 0,
           label = "傷病者世帯", 
           size = annotate_size , 
           color = "gray30", 
           family = font_name) +
  annotate(geom = "text", 
           x = as.Date("2020-07-01"), 
           y = 0, 
           hjust = 0,
           label = "その他の世帯", 
           size = annotate_size , 
           color = "gray30", 
           family = font_name)

YOY_public_assistance <- YOY_public_assistance +
  labs(title="パネルB:利用世帯数(世帯類型別、前年同月差)", 
       x = "月日",
       y = "")  +
  theme(text = element_text(size = 10))

YOY_public_assistance

YOY_public_assistance


## グラフの統合と表示

```r
graph_hogo_combined <- patchwork::wrap_plots(graph_hogo_zenkoku47_persons_receive, 
                      YOY_public_assistance, nrow = 1)
graph_hogo_combined

6.2 グラフの保存

width_size <- 9.2

graph_hogo_combined %>% 
  ggsave(filename="output/graph_hogo_combined.png",
         width= width_size,height= width_size*0.5,
         dpi = 600)

6.3 インタラクティブグラフ

6.3.1 パネルA:10万人あたり利用者数

ggplotly(graph_hogo_zenkoku47_persons_receive)
## Warning in geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]): geom_GeomTextRepel() has yet to be implemented in plotly.
##   If you'd like to see this geom implemented,
##   Please open an issue with your example code at
##   https://github.com/ropensci/plotly/issues

6.3.2 パネルB:生活保護(世帯類型別)

ggplotly(YOY_public_assistance)